﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication2.Class;
using System.Windows.Input;
using System.Collections;
using System.Diagnostics;
using Newtonsoft.Json;

namespace WebApplication2
{
    public partial class PatientList : System.Web.UI.Page
    {
        int language = 0;
        int PatientID = 0;
        string patientStatus = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            language = (int)Session["Language"];
            System.Web.UI.HtmlControls.HtmlGenericControl currdiv = (System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("menu");
            currdiv.Visible = false;

            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("hi-IN");
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("hi-IN");
            Deserialize_json_Object_and_Bind_GridView();
           
           
        }
        public void Deserialize_json_Object_and_Bind_GridView()
        {
            WebRequest req = WebRequest.Create("http://192.168.1.156/api/Patients");
            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
            json = "{\"Patients\":" + json + "}";

            JavaScriptSerializer ser = new JavaScriptSerializer();
            var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapperPatients>(json);

           // List<KeyWrapperPatients> p = new List<KeyWrapperPatients>();
          
              GridView1.DataSource = keysWrapper.Patients;
             
             GridView1.DataBind();

             Gender();
             Color();
        }
        public void Deserialize_json_Object_and_Bind_GridView2()
        {
            

            PatientID = (int)Session["PatientId"];
            WebRequest req = WebRequest.Create("http://192.168.1.156/api/TreatmentList?PatientID=" + PatientID );
            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
            json = "{\"TreatmentList\":" + json + "}";

            JavaScriptSerializer ser = new JavaScriptSerializer();
            var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapperTreatmentList2>(json);

            
            GridView2.DataSource = keysWrapper.TreatmentList;
            GridView2.DataBind();
            Color2();
        }

        protected void Gender()
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                string gender = GridView1.Rows[i].Cells[3].Text;
                if (gender.CompareTo("Male")==1)
                {
                    GridView1.Rows[i].Cells[3].Text = "M";
                }
               else
                {
                    GridView1.Rows[i].Cells[3].Text = "F";
                }
            }
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            patientStatus = (string)Session["patientStatus"];
            if (e.Row.RowType == DataControlRowType.Header)
            {

                if (e.Row.Cells.Count > 0)
                {
                    WebRequest req = WebRequest.Create("http://192.168.1.156/api/HDTreatmentList?LanguageID=" + language);
                    req.ContentType = "application/json";
                    WebResponse resp = req.GetResponse();
                    Stream stream = resp.GetResponseStream();
                    StreamReader re = new StreamReader(stream);
                    String json = re.ReadToEnd();
                    json = "{\"HDTreatmentList\":" + json + "}";

                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    var KeyWrapperTreatmentList = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapperTreatmentList>(json);

                    if (Deserialize_json_Object_ControlParameter() == true)
                    {
                        GridView1.Columns[1].Visible = false;
                    }
                    else
                    {
                        GridView1.Columns[2].Visible = false;
                    }
                     foreach (var item in KeyWrapperTreatmentList.HDTreatmentList)
                     {
                         if (item.FormLabelCode == "HD Treatment")
                         {
                             lblHeader.Text = item.Language.ToString();
                         }

                         if (item.FormLabelCode == "Patlist-Title")
                      {
                           lblPatients.Text = item.Language.ToString();

                       }

                         if (item.FormLabelCode == "PatFilter-Act")
                         {
                            
                             if (patientStatus == "Active")
                             {
                                 lblStataus.Text = item.Language.ToString();
                             }
                         }
                         if (item.FormLabelCode == "PatFiltert-Pas")
                         {
                           //  patientStatus = (string)Session["patientStatus"];
                             if (patientStatus == "Inactive")
                             {
                                 lblStataus.Text = item.Language.ToString();
                             }
                         }
                         if (item.FormLabelCode == "PaFilter-Exp")
                         {
                            // patientStatus = (string)Session["patientStatus"];
                             if (patientStatus == "Expired")
                             {
                                 lblStataus.Text = item.Language.ToString();
                             }
                         }
                    //    if (item.FormLabelCode == "Patlist-MRN")
                    //    {
                    //        //if (Deserialize_json_Object_ControlParameter() == true)
                    //        //{

                    //        //}
                    //        //else
                    //        //{
                    //            e.Row.Cells[1].Text = item.Language.ToString();
                    //      //  }

                    //    }

                    //    if (item.FormLabelCode == "Patlist-PID")
                    //    {

                    //        //if (Deserialize_json_Object_ControlParameter() == true)
                    //        //{
                    //            e.Row.Cells[2].Text = item.Language.ToString();
                    //        //}
                    //        //else
                    //        //{
                    //        //}

                    //    }

                    //    if (item.FormLabelCode == "Patlist-Sex")
                    //    {
                    //        e.Row.Cells[3].Text = item.Language.ToString();
                    //    }


                    }
                }
                
            }
            if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
            {

                // when mouse is over the row, save original color to new attribute, and change it to highlight color
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#FFCCCC'");

                // when mouse leaves the row, change the bg color to its original value  
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor= this.originalstyle;");
               // e.Row.Attributes.Add("onclick", "this.style.backgroundColor= '#FF6699';");
                e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.DataItemIndex, true);
              
            }
           

        }

        //protected override void Render(System.Web.UI.HtmlTextWriter writer)
        //{
        //    foreach (GridViewRow row in GridView1.Rows)
        //    {
        //        if (row.RowType == DataControlRowType.DataRow)
        //        {
        //            // when mouse is over the row, save original color to new attribute, and change it to highlight color
        //            row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#FFFF00'");

        //            // when mouse leaves the row, change the bg color to its original value  
        //            row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
        //            row.Attributes["onclick"] =
        //             ClientScript.GetPostBackClientHyperlink(GridView1,
        //                "Select$" + row.DataItemIndex, true);
        //        }
        //    }
        //    base.Render(writer);
        //}

        public void Color()
        {
            //string status = ((HiddenField)GridView1.SelectedRow.Cells[5].FindControl("patient_status1")).Value;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                
                string status = GridView1.Rows[i].Cells[4].Text;
                if (status =="Active")
                {
                    GridView1.Rows[i].BackColor = System.Drawing.Color.White;
                }
                if (status =="Inactive")
                {
                    GridView1.Rows[i].BackColor = System.Drawing.Color.Wheat;
                }
                if (status == "Expired")
                {
                    GridView1.Rows[i].BackColor = System.Drawing.Color.LightGray;
                }
            }
        }

        public void Color2()
        {
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                int s = Convert.ToInt32(GridView2.Rows[i].Cells[7].Text);
                if (s > 0)
                {
                    GridView2.Rows[i].Cells[7].ForeColor = System.Drawing.Color.Red;
                }

            }
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
           // int PatientId = Convert.ToInt32(GridView1.SelectedRow.Cells[4].Text);
            int rowIndex = GridView1.SelectedIndex;
            int PatientId = Convert.ToInt32(GridView1.DataKeys[rowIndex].Value.ToString());
            string patientStatus = GridView1.Rows[rowIndex].Cells[4].Text.ToString();
           
           // Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "SCRIPT", string.Format("alert('{0}');", PatientId), true);
            Session["PatientId"] = PatientId;
           // lblStataus.Text = patientStatus;
            GridView1.SelectedRow.BackColor = System.Drawing.Color.LightPink;
            Session["patientStatus"] = patientStatus;
            Deserialize_json_Object_and_Bind_GridView2();
        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int rowIndex = GridView2.SelectedIndex;
            string TreatmentDate = GridView2.Rows[rowIndex].Cells[0].Text.ToString();
            int hdtreatmentid = Convert.ToInt32(GridView2.Rows[rowIndex].Cells[10].Text.ToString());
            int patientid = Convert.ToInt32(GridView2.Rows[rowIndex].Cells[11].Text.ToString());
            string arrivaltime = GridView2.Rows[rowIndex].Cells[12].Text.ToString();
            
            DataForSummaryScreen DFSS = new DataForSummaryScreen();
            DFSS.PatientID = patientid;
            DFSS.Arrival_Time = arrivaltime;
           // arrivaltime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            Session["DataForSummaryScreen"] = DFSS;
            Session["treatmentID"] = hdtreatmentid;
            lblName.Text = TreatmentDate;
            Session["Language"] = language;
            Response.Redirect("Summary.aspx?");
        }

        //Deserialize_json_Object_ControlParameter
        public bool Deserialize_json_Object_ControlParameter()
        {
            bool result = true;
            WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXControlParameters");
            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
           // json = "{\"ControlParameters\":" + json + "}";
          //  json = "{\"ControlParameters\":" + json + "}";
           JavaScriptSerializer ser = new JavaScriptSerializer();
           List<ControlParameters> keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ControlParameters>>(json);
           // ControlParameters response_obj = new ControlParameters();

           // var wholeObject = ser.Deserialize<KeyWrapperPara>(json);
           
          
          
          //  if (keysWrapper.para != null)
         //   {
            foreach (var item in keysWrapper)
                {
                    if (item.ParamaterID == 23 && item.ParameterBoolean == true)
                    {
                        result = true;
                    }
                    result = false;
                }
           // }
            return result;
        }


        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    if (e.Row.Cells.Count > 0)
                    {
                        WebRequest req = WebRequest.Create("http://192.168.1.156/api/HDTreatmentList?LanguageID=" + language);
                        req.ContentType = "application/json";
                        WebResponse resp = req.GetResponse();
                        Stream stream = resp.GetResponseStream();
                        StreamReader re = new StreamReader(stream);
                        String json = re.ReadToEnd();
                        json = "{\"HDTreatmentList\":" + json + "}";

                        JavaScriptSerializer ser = new JavaScriptSerializer();
                        var KeyWrapperTreatmentList = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapperTreatmentList>(json);

                        foreach (var item in KeyWrapperTreatmentList.HDTreatmentList)
                        {
                            if (item.FormLabelCode == "Txlist-Txdate")
                            {
                                e.Row.Cells[0].Text = item.Language.ToString();
                                e.Row.Cells[0].Width = 150;
                            }

                            if (item.FormLabelCode == "Txlist-Atime")
                            {
                                e.Row.Cells[1].Text = item.Language.ToString();
                            }
                            if (item.FormLabelCode == "Txlist-runno")
                            {
                                e.Row.Cells[2].Text = item.Language.ToString();
                            }

                            if (item.FormLabelCode == "Txlist-dur")
                            {
                                e.Row.Cells[3].Text = item.Language.ToString();
                            }

                            if (item.FormLabelCode == "Txlist-prewt")
                            {
                                e.Row.Cells[4].Text = item.Language.ToString();
                            }
                            if (item.FormLabelCode == "Txlist-postwt")
                            {
                                e.Row.Cells[5].Text = item.Language.ToString();
                            }
                            if (item.FormLabelCode == "Txlist-Netuf")
                            {
                                e.Row.Cells[6].Text = item.Language.ToString();
                            }
                            if (item.FormLabelCode == "Txlist-Comp")
                            {
                                e.Row.Cells[7].Text = item.Language.ToString();
                            }
                            if (item.FormLabelCode == "Txlist-Tloc")
                            {
                                e.Row.Cells[8].Text = item.Language.ToString();
                            }
                            if (item.FormLabelCode == "Txlist-signoff")
                            {
                                e.Row.Cells[9].Text = item.Language.ToString();
                            }
                            if (item.FormLabelCode == "Txlist-Title")
                            {
                                lblTreatmentList.Text = item.Language.ToString();
                            }
                            
                        }
                    }
                
            }
            if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
            {

                // when mouse is over the row, save original color to new attribute, and change it to highlight color
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#FFCCCC'");

                // when mouse leaves the row, change the bg color to its original value  
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
                e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView2, "Select$" + e.Row.DataItemIndex, true);
            }

        }

        //protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    GridViewRow row = GridView1.SelectedRow;

        //    index = Convert.ToInt32(row.Cells[5].Text);
        //    Deserialize_json_Object_and_Bind_GridView2(); 
        //}

        //protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    PatientID = 53;
        //    WebRequest req = WebRequest.Create("http://192.168.1.156/api/TreatmentList?PatientID=" + PatientID);
        //    req.ContentType = "application/json";
        //    WebResponse resp = req.GetResponse();
        //    Stream stream = resp.GetResponseStream();
        //    StreamReader re = new StreamReader(stream);
        //    String json = re.ReadToEnd();
        //    json = "{\"TreatmentList\":" + json + "}";

        //    JavaScriptSerializer ser = new JavaScriptSerializer();
        //    var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<KeyWrapperTreatmentList2>(json);
        //    GridView2.DataSource = keysWrapper.TreatmentList;
        //    GridView2.DataBind();

        //}

        //public void GridView2_CellDoubleClick(object sender, MouseButtonEventArgs e)
        //{
        //    PatientID = 53;

        //    Deserialize_json_Object_and_Bind_GridView2();



        //}

        // protected void GridView2_DblClick(object sender, ClickEventArgs e)
        //{

        //      int row_index = 0;

        //     string meter_num=System.String.Empty;


        //     row_index = int.Parse(e.Cell.Row.Index.ToString()); // this works
        //     meter_num = this.GridView2.Rows[row_index].Cells[0].Text.ToString();


        //}

       
    }
}